package com.wanxi.servlet;

import com.wanxi.model.ProductModel;
import com.wanxi.service.ProductService;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/findById")
public class ProductFindByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2
        String id = req.getParameter("id");
//        3
        ProductModel productModel = ProductModel.builder()
                .id(Tool.strToInt(id))
                .build();
//        4
        ProductService productService = new ProductService();
        ProductModel model = productService.findById(productModel);
//        5
        req.setAttribute("model", model);
        req.getRequestDispatcher("/update.jsp").forward(req, resp);
    }
}
